{include file="public/header"}
        <style scoped>
            .el-select-dropdown {
            padding: 0;
            }
        </style>
         {include file="public/style"}
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>仓库管理</el-breadcrumb-item>
            <el-breadcrumb-item>仓库列表</el-breadcrumb-item>
        </el-breadcrumb>
        <br>
        <el-card >
            <div style="display: flex;align-items: center;justify-content: flex-start;">
                <el-form :inline="true" :model="searchForm" size="mini"  ref="refsearchForm">                
                   <el-form-item label="仓库类型">                     
                        <el-select v-model="searchForm.is_default" placeholder="请选择仓库类型" filterable clearable>
                            <el-option v-for="item in is_default" :key="item.value" :label="item.label" :value="item.value">
                            </el-option>
                        </el-select>      
                    </el-form-item>         
   
                    <el-form-item label="仓库状态">
                        <el-select v-model="searchForm.status" placeholder="请选择仓库状态" filterable clearable>
                            <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item> 
     

                    <el-form-item>
                        <el-button type="primary"  size="mini" plain @click="getSearch" icon="el-icon-search"v-cloak>查询</el-button>
                    </el-form-item>          
                  </el-form>
                
                <div style="margin-left: auto;margin-bottom: 20px;margin-right: 20px;" >
                        <el-button  type="primary"   @click="create" size="mini"   icon="el-icon-edit">添加仓库</el-button>       
                </div>
            </div>

            <el-table :data="tablelist"   style="width: 100%;margin-bottom: 20px;"  row-key="id"  border  :row-key="(row)=>row.key">
                <el-table-column   prop="id" label="ID" width="80"> </el-table-column>
                <el-table-column   prop="name" label="仓库昵称" width="280"> </el-table-column>                
                <el-table-column   prop="is_default" label="仓库类型">
                    <template v-slot="{row}">
                        <span v-if="row.is_default===1">
                            主仓库
                            </span>
                            <span   v-if="row.is_default===0">
                            样品仓                                               
                        </span>   
                    </template>       
                </el-table-column>   
                
                <el-table-column   prop="status"  label="仓库状态" > 
                    <template v-slot="{row}">
                        <span v-if="row.status===1">
                            <el-tag type="success">启用</el-tag>
                            </span>
                            <span   v-if="row.status===0">
                            <el-tag type="danger">禁用</el-tag>                                                
                        </span>   
                    </template>                          
                </el-table-column>
                <el-table-column   prop="createtime" label="创建时间" > </el-table-column>  
                <el-table-column  fixed="right" label="操作" >
                    <template v-slot="{row}">
                        <el-button type="primary"  plain  icon="el-icon-edit"   @click="editor(row)" >编辑 </el-button>
                        <el-button type="danger"  plain icon="el-icon-delete"   @click="cancel(row.id)">删除</el-button>             
                    </template>
                </el-table-column>
            </el-table>

            <!-- page -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="searchForm.pagenum" :page-sizes="[20,50,100, 150, 300,500,1000,2000]"
                :page-size="searchForm.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>  
        </el-card>   
        
        
            
        <!-- 修改编辑弹窗 -->
        <el-dialog :title="title" :visible.sync="isstroageVisible " width="600px" :close-on-click-modal="false">
            <el-form :inline="true" :model="createForm" size="mini"  :rules="isStroagerules"  ref="refsearchForm"  :popper-append-to-body="false"  >              
          
                <el-form-item prop="name" label="仓库名称">
                    <el-input v-model="createForm.name" placeholder="请输入仓库名称" style="width: 370px;"
                            clearable></el-input>
                </el-form-item>              

                <el-form-item prop="url" label="仓库类型">
                    <template   >                       
                        <el-radio-group v-model="createForm.is_default" style="margin-left: 10px;">
                            <el-radio :label="1" border>主仓库</el-radio>
                            <el-radio :label="0" border>样品仓</el-radio>
                    </el-radio-group> 
                    </template>
                    
                </el-form-item>   
                <div>      
                    <el-form-item  label="仓库状态:">                                   
                        <el-radio-group v-model="createForm.status"  style="margin-left: 10px;">         
                            <template>            
                            <el-radio :label="1"  >正常</el-radio>   
                            <el-radio :label="0"  >关闭</el-radio>                     
                        </template>
                        </el-radio-group>                
                    </el-form-item> 
                </div>          
             
                <div>                                   
                    <el-form-item>
                        <el-button type="primary" @click="checkSubmits">确认</el-button>
                        <el-button type="primary" plain @click="close"> 取消</el-button>
                    </el-form-item>
                </div>
            </el-form>
        </el-dialog>    
    </div>

    <script>
        new Vue({
            el: '#app',
            data: function () {
                return {    
                    title:'',   
                    isstroageVisible:false,
                    status:[
                        {
                            value: 2,
                            label: '关闭'
                        },
                        {
                            value: 1,
                            label: '正常'
                        }
                    ],// 状态是否正常       
                    is_default:[
                        {
                            value: 1,
                            label: '主仓库'
                        },
                        {
                            value: 2,
                            label: '样品仓'
                        }
                    ],//仓库类型
                    total:0,   //分页总页数      
                    tablelist:[] ,              
                    searchForm: {              
                        is_default: '',
                        status: '',
                        pagenum: 1,
                        pagesize: 20, 
                    },      
                    createForm:{
                        status:1,
                        is_default:'1',
                        name:'',               
                    },
                    isStroagerules: {                      
                        name: [
                            { required: true, message: '请填写仓库名称', trigger: 'blur' },
                            
                        ],
                    
                    }
                }
            },
            mounted(){        
                this.getSearch()
            },
            methods: {        

                async getDatalist() {
                    const res = await axios.post('/admin/stroages/index', {params: this.searchForm})
                        if (res.status !== 1) {
                            return this.$message.error(res.info);
                        }
                    this.total = res.info.total
                    this.tablelist = res.info.list

                },
                    
                handleSizeChange(newSize) {
                    this.searchForm.pagesize = newSize
                    this.getDatalist()
                },
                handleCurrentChange(newPage) {
                    this.searchForm.pagenum = newPage
                    this.getDatalist()
                },
                    
                getSearch() {
                    this.searchForm.pagenum = 1
                    this.getDatalist()
                },
            
                    //编辑
                editor(row){
                    this.title = '编辑仓库'
                    this.isstroageVisible = true
                    this.createForm=JSON.parse(JSON.stringify(row));             
                },
                //添加权限
                create(){  
                    this.title = '添加仓库'
                    this.isstroageVisible = true
                    this.createForm.id = ''
                    this.createForm.status = 1
                    this.createForm.is_default = 1
                    this.createForm.name = ''             
                },                  
                //确认提交修改
                async checkSubmits(){
                    this.$refs.refsearchForm.validate(async valid => {
                        if (!valid) return;
                        let loading = this.$loading({
                            lock: true,
                            text: '数据修改中···',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.7)'
                        });                

                    axios.post('/admin/stroages/create', {params: this.createForm}).then(res => {
                            loading.close()
                            if (res.status !== 1) {
                                return this.$message.error(res.info);                  
                            }
                        
                            this.$message.success('操作成功');
                            this.isstroageVisible = false
                            this.getDatalist()
                        }).catch(err => {
                            console.log(err)
                        })
                    })
                },    //删除
                cancel($id){
                    this.$confirm('确认需要删除？删除了无法恢复', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        closeOnClickModal: false, // 禁用点击遮罩关闭弹窗
                        type: 'warning'
                    }).then(() => {
                        axios.post('/admin/stroages/delete', {params: {id: $id}}).then(res => {
                            if (res.status !== 1) {
                                return this.$message.error(res.info);             
                            }                    
                            this.$message.success('删除成功');
                            this.getDatalist()
                        }).catch(err => {
                            console.log(err)
                        })
                    }).catch(() => {
                        this.$message.info('已取消');
                    })
                },
                //关闭弹窗
                close(){
                    this.isstroageVisible=false  
                }, 
            }   
        })
    </script>
</body>  
</html>
